package com.lanou.beautifulwardrobe.brand;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.baoyz.widget.PullRefreshLayout;
import com.google.gson.Gson;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.view.Tools;
import com.lanou.beautifulwardrobe.contants.Contants;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.EditRecommendBrand;
import com.lanou.beautifulwardrobe.entify.EditRecommendResult;
import com.lanou.beautifulwardrobe.entify.RiseResult;
import com.lanou.beautifulwardrobe.brand.adapter.BrandRiseAdapter;


import java.util.List;

/**
 * Created by dllo on 15/10/26.
 */
public class BrandFragment extends Fragment {

    private VolleySingleton volleySingleton = VolleySingleton.getInstance();

    private GridLayoutManager manager;
    private GridLayoutManager editManager;
    private RecyclerView recyclerView;
    private BrandRiseAdapter adapter;
    private PullRefreshLayout refreshableView;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.maintab_brand_item, null);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        recyclerView = (RecyclerView) view.findViewById(R.id.brandrecyclerView);
        refreshableView= (PullRefreshLayout) view.findViewById(R.id.refreshableView);
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Tools.getProgress(getActivity());
        initView();
        initData();

    }

    private void initView() {
        manager = new GridLayoutManager(getActivity(), 1);
        adapter = new BrandRiseAdapter();

        refreshableView.setRefreshStyle(PullRefreshLayout.STYLE_WATER_DROP);
        refreshableView.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshableView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Tools.getProgress(getActivity());
                        initData();
                        refreshableView.setRefreshing(false);
                    }
                }, 0);
            }
        });
    }
    private void initData() {

        volleySingleton.getStringRequset(Contants.BRAND_EDIT_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                EditRecommendResult editRecommendResult = gson.fromJson(response, EditRecommendResult.class);
                if (editRecommendResult.getMsg().equals("ok")) {
                    List<EditRecommendBrand> brands=editRecommendResult.getData().get(0).getBrands();
                    adapter.addRankData(brands);
                }
            }
        });
        recyclerView.setLayoutManager(manager);
        volleySingleton.getStringRequset(Contants.BRAND_RISE_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                RiseResult riseResult = gson.fromJson(response, RiseResult.class);
                if (riseResult.getMsg().equals("ok")) {
                    adapter.addData(riseResult.getData());
                    Tools.progressDialog.dismiss();
                    recyclerView.setAdapter(adapter);
                }
            }
        });

    }
}